using System;
using System.Data;
using System.Data.SqlClient;
using Prologis.DAL;


namespace Prologis.ShifterNT.Datasink
{

    public class DbDataSink : IDataSink
    {

        public IDbHelperWrapper DbHelper { get; set; }
        public string ConnectionString { get; set; }
        public string ProviderName { get; set; }
        public string TableName { get; set; }
        public string ClearTable { get; set; }
        public string SpName { get; set; }
        public bool UseTransaction { get; set; }
        



        bool active = false;
        public bool Active
        {
            get { return active; }
            set
            {
                if (active != value)
                {
                    active = value;

                    if (active)
                    {
                        this.DbHelper = DbHelperFactory.GetDbHelper(this.ConnectionString, this.ProviderName);
                    }
                    else
                    {
                        //TODO: Nullsetzen notwendig ?
                        this.DbHelper = null;
                    }
                }
            }
        }




        /// <summary>
        /// Sets the paras.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="arrParam">The arr param.</param>
        /// <returns></returns>
        public IDataParameter[] SetParas(FieldList data, IDataParameter[] arrParam)
        {

            foreach (IDataParameter p in arrParam)
            {
                if (p.Direction == ParameterDirection.Output)
                {
                    p.Value = DBNull.Value;
                }
                else
                {
                    string name = p.ParameterName.Substring(1);

                    object val = null;
                    object def = null;

                    if (data.ContainsKey(name))
                    {
                        val = data[name].Value;
                        def = data[name].Default;
                    }
                    else
                    {
                        if (data.ContainsKey(name.ToUpper()))
                        {
                            val = data[name.ToUpper()].Value;
                            def = data[name].Default;
                        }
                    }

                    //Wir haben Wert
                    if (val != null)
                    {
                        p.Value = val;
                        //Wir haben keinen Wert aber default
                    }
                    else if (val == null && def != null)
                    {
                        p.Value = def;
                        //Wir haben keinen Wert --> DBNull
                    }
                    else if (val == null && def == null)
                    {
                        p.Value = DBNull.Value;
                    }

                }

            }

            return arrParam;
        }


        /// <summary>
        /// Takes the specified data.
        /// </summary>
        /// <param name="data">The data.</param>
        public void Take(FieldList data)
        {
            try
            {

                IDataParameter[] arrEmptyParam = DbHelper.GetSpParameterSet(SpName, true);
                IDataParameter[] arrParam = SetParas(data, arrEmptyParam);
                DbHelper.ExecuteNonQuery(CommandType.StoredProcedure, SpName, arrParam);

            }
            catch (Exception e)
            {
                throw new Exception("Last insert command failed! Fieldlist: " + data.ToString(), e);
            }
        }


    }

}



